import { http } from "@/utils/http";
import type { Namespace, NamespaceList } from "./types";
import type { BaseResponse, PromiseRes } from "@/api/response";
import {
  k8sPathMergeReqs,
  type MergePathReq,
  type MakePathReq
} from "./apiHelper";

export class K8sNamespace {
  baseReq: MakePathReq = {
    src: "namespaces",
    app: ""
  };

  getNamespaceList = (query?: MergePathReq): BaseResponse<NamespaceList> => {
    const url = k8sPathMergeReqs(this.baseReq);

    return http.get(url, { params: query });
  };

  getNamespaceDetail = (
    query: MergePathReq,
    data?: Namespace
  ): BaseResponse<Namespace> => {
    const api = k8sPathMergeReqs(query, this.baseReq, {
      namespace: "",
      srcName:
        query?.srcName ||
        data?.metadata?.name ||
        query.namespace ||
        data?.metadata?.namespace
    });
    return http.get(api, { params: query.query });
  };

  updateNamespace = (query: MergePathReq, data?: Namespace): BaseResponse => {
    const api = k8sPathMergeReqs(query, this.baseReq, data);
    return http.put(api, { data });
  };

  deleteNamespace = (query: MergePathReq, data?: Namespace): BaseResponse => {
    const api = k8sPathMergeReqs(query, this.baseReq, data);
    return http.delete(api, { data });
  };

  createNamespace = (query: MergePathReq, data?: Namespace): PromiseRes => {
    const api = k8sPathMergeReqs(query, this.baseReq);
    return http.post(api, { data });
  };
}
